package com.bum.rss.site;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.InitializingBean;

import com.bum.rss.exp.om.RssEntry;
import com.bum.rss.site.cat.H24Cat;
import com.bum.rss.site.util.BaseParser;

public class H24Site extends Site implements H24Cat, InitializingBean {
	final HashMap<String, String>	CAT_CODES	= new HashMap<String, String>();
	final List<RssEntry>			CATEGORIES	= new ArrayList<RssEntry>(CATS.length);

	@Override
	public void afterPropertiesSet() throws Exception {
		for (int n = CATS.length, i = 0; i < n; i++) {
			CAT_CODES.put(CATS[i][0], CATS[i][2]);
			CATEGORIES.add(new RssEntry(getCode() + URL_SEPARATOR + CATS[i][0], CATS[i][1]));
		}
	}

	@Override
	public List<RssEntry> listSiteCats(String user) {
		return CATEGORIES;
	}

	@Override
	public List<RssEntry> listCatEntrys(String cat, String user) {
		int page = 1;
		if (cat.indexOf(PAGE_SEPARATOR) > 0) {
			String[] parts = StringUtils.split(cat, PAGE_SEPARATOR);
			page = NumberUtils.toInt(parts[1], 1);
			cat = parts[0];
		}

		String url = "http://hcm.m.24h.com.vn/a-c" + CAT_CODES.get(cat) + ".html?start=" + (page - 1) * 15;
		String html = BaseParser.loadHtml(url, BaseParser.ANDROID_HEADERS, true);
		html = BaseParser.parseContent(html, "<div class=\"tin-anh\">", "<div class=\"phan-trang\">");

		List<RssEntry> entries = new LinkedList<RssEntry>();

		html = html.replace("<h3>", "<b>");
		String[] parts = html.split("<div class=\"tin-anh\">");
		for (int i = 0; i < parts.length; i++) {
			String part = parts[i];
			String link = BaseParser.parseContent(part, "<a href=\"", "\"");
			if (link == null) {
				continue;
			}
			if (!link.startsWith("http://")) {
				link = "http://hcm.m.24h.com.vn/" + link;
			}

			link = getRssLink(link, cat);
			if (link == null) {
				continue;
			}

			String img = BaseParser.parseContent(part, "src=\"", "\"");
			
			part = part.substring(part.indexOf("<b> <a href"));
			String title = BaseParser.parseContent(part, "\">", "</a>");
			if (title == null) {
				continue;
			}
			title = title.trim();
			
			String intro = BaseParser.parseContent(part, "<p>", "</p>");

			RssEntry entry = new RssEntry(link, title);
			entry.setThumb(img);
			entry.setIntro(intro);

			entries.add(entry);
		}

		if (entries.size() >= 9) {
			entries.add(new RssEntry(getCode() + URL_SEPARATOR + cat + PAGE_SEPARATOR + (page + 1), NEXT_PAGE_TITLE));
		}

		if (page > 1) {
			entries.add(0, new RssEntry(getCode() + URL_SEPARATOR + cat + PAGE_SEPARATOR + (page - 1), PREV_PAGE_TITLE));
		}

		return entries;
	}

	@Override
	public String getRssLink(String httpLink) {
		return getRssLink(httpLink, "m");
	}

	// http://attitude.co.uk/ben-whishaw-im-worried-gay-typecasting/ => rss://att/m/ben-whishaw-im-worried-gay-typecasting
	protected String getRssLink(String httpLink, String catId) {
		if (StringUtils.isBlank(httpLink)) {
			return null;
		}

		int idx = httpLink.lastIndexOf("a");
		if (idx < 0) {
			return null;
		}
		idx += 1;

		int endIdx = httpLink.indexOf(".html");
		if (endIdx < 0) {
			return null;
		}

		String code = httpLink.substring(idx, endIdx);
		if (StringUtils.isBlank(code)) {
			return null;
		}

		return getCode() + URL_SEPARATOR + catId + URL_SEPARATOR + code;
	}

	@Override
	public RssEntry loadEntry(String path, String user) {
		String url = "http://hcm.m.24h.com.vn/a/a-c70a" + path + ".html";

		String html = BaseParser.loadHtml(url, BaseParser.ANDROID_HEADERS, true);
		url = "http://hcm.m.24h.com.vn" + BaseParser.parseContent(html, "location.href=\"", "\"");

		html = BaseParser.loadHtml(url, BaseParser.ANDROID_HEADERS, true);
		html = BaseParser.parseContent(html, "<div class=\"tin-anh\">", "</div> </div> <div class=\"tin-anh");

		String title = BaseParser.parseContent(html, "<span class=\"bv-tieude\">", "</span>");
		if (StringUtils.isBlank(title)) {
			return null;
		}
		title = title.trim();

		html = BaseParser.parseContent(html, "<div class=\"bv-date\">", null);

		RssEntry entry = new RssEntry();
		entry.setTitle(title);
		entry.setContent(html);

		return entry;
	}

	public static void main(String[] args) throws Exception {
		H24Site site = new H24Site();
		site.afterPropertiesSet();

//		RssEntry entry = site.loadEntry("647968", null);
//		System.out.println(entry.getTitle());
//		System.out.println(entry.getContent());

		List<RssEntry> entries = site.listCatEntrys("i", null);
		if (entries != null) {
			System.out.println(entries.size());
			for (RssEntry e : entries) {
				System.out.println(e.getUrl() + " " + e.getTitle());
			}
		}
	}
}
